<template>
    <a-drawer title="模型参数" placement="right" v-model:visible="drawer_show" @close="onClose" width="900px"
        class="white-drawer" style="user-select: none">
        <template #extra v-if="show_extra_bts">
            <a-tooltip placement="bottom" v-if="show_extra_type=='Pre_Match_Params_Extra'">
                <template #title>RPC误差很大时推荐粗匹配参数</template>
                <a-button @click="update_Pre_Match_Ba_Params" style="margin: 0 auto;margin-right: 10px;">
                    <template #icon><unordered-list-outlined /></template>
                </a-button>
            </a-tooltip>
            <a-tooltip placement="bottom" v-if="show_extra_type == 'Pre_Ba_Params_Extra'">
                <template #title>RPC误差很大时推荐粗平差参数</template>
                <a-button @click="update_Pre_Match_Ba_Params" style="margin: 0 auto;margin-right: 10px;">
                    <template #icon><unordered-list-outlined /></template>
                </a-button>
            </a-tooltip>
        </template>
        <parmars_div :parmars="intputs?.vals" :type="intputs?.type" @save_parmars="save_parmars" v-if="isSingle" />
        <a-tabs v-model:activeKey="tabs_activeKey" class="white-collapse" v-if="isSingle==false" @change="tabs_change">
            <a-tab-pane :key="index" v-for="(record, index) in intputs">
                <a-row v-if="!(record?.hide === true) ">
                    <parmars_div :parmars="record?.vals" :type="record?.type" @save_parmars="save_parmars"
                        @select_parmar_change="select_parmar_change" />
                </a-row>
                <template #tab>
                    <span>{{ record?.name }}
                        <a-tooltip placement="right" v-if="record?.tip">
                            <template #title>{{ record?.tip }}</template>
                            <exclamation-circle-outlined />
                            <!-- style="color: red;" -->
                        </a-tooltip>
                    </span>
                </template>
            </a-tab-pane>
        </a-tabs>
        <div :style="{
                bottom: 0,
                width: '100%',
                borderTop: '1px solid #e8e8e8',
                padding: '10px 16px',
                textAlign: 'right',
                left: 0,
                borderRadius: '0 0 4px 4px',
                'margin-top': '10px',
            }">
            <a-space>
                <a-button style="margin-right: 8px" @click="onClose"> 取消 </a-button>
                <a-button type="primary" @click="onOk"> 保存 </a-button>
            </a-space>
        </div>
    </a-drawer>
</template>
<script>
import { defineAsyncComponent } from 'vue'
export default {
    components: {},
    data() {
        return {
            drawer_show: false,
            isSingle:true,
            tabs_activeKey:0,
            show_extra_bts:false,
            show_extra_type:"",
            intputs:undefined,
            changed:{},

            //mitt
            mitt_show_parmars_drawer_id:"",
            mitt_show_parmars_drawer_back_id:"",

        };
    },
    beforeUnmount() {
        this.$mitt.off('show_parmars_drawer');
    },
    components: {
        parmars_div: defineAsyncComponent(() => import('@/components/common/parmars_div.vue')),
    },
    mounted() {
        console.log("parmars_drawer")
        this.mitt_show_parmars_drawer_id=this.$mitt.on('show_parmars_drawer', (uid,options) => {
            this.mitt_show_parmars_drawer_back_id=uid
            this.drawer_show = true;
            this.changed={}
            if(Array.isArray(options?.intput)){
                this.intputs= options?.intput
                this.isSingle = false
                this.tabs_change(0)
            }
            else{
                this.intputs = options?.intput
                this.isSingle=true
            }
        });
    },
    methods: {
        tabs_change(activeKey){
            if (this.intputs[activeKey]?.extravals){
                this.show_extra_type=this.intputs[activeKey]?.extratype
                this.show_extra_bts=true
            }
            else{
                this.show_extra_type =""
                this.show_extra_bts=false
            }
        },
        update_Pre_Match_Ba_Params(){
            let intput = this.intputs[this.tabs_activeKey]
            let extravals = intput?.extravals
            let vals = intput?.vals
            for (let i = 0; i < extravals?.length; i++) {
                let one_extraval = extravals[i];
                let find_index = vals?.findIndex((val)=>{
                    return val?.name == one_extraval?.name
                })
                if (find_index>=0){
                    this.intputs[this.tabs_activeKey].vals[find_index].default = one_extraval?.default
                }
            }
        },
        save_parmars(props){
            let type = props?.type
            let parmars = props?.parmars
            this.changed[type]= parmars
        },
        select_parmar_change(values){
            let { type, value } = values
            for (let i = 0; i < this.intputs?.length; i++) {
                let one_intput = this.intputs[i];
                let set_hide=false
                if (type == "zcmatch" && value==0){
                    if (one_intput?.type == "Self_Pre_Match_Params" || one_intput?.type == "Self_Pre_Ba_Params") {
                        set_hide=true
                    }
                }
                this.intputs[i].hide = set_hide
            }
        },
        onOk() {
            let _show_parmars_drawer_result=[]
            for (const [key, value] of Object.entries(this.changed)) {
                let one_show_parmars_drawer_result = {}
                one_show_parmars_drawer_result.type = key
                one_show_parmars_drawer_result.vals = this.$common_apis.parse_value_to_ini(value)
                _show_parmars_drawer_result.push(one_show_parmars_drawer_result)
            }
            this.$mitt.oemit('show_parmars_drawer_back',_show_parmars_drawer_result,this.mitt_show_parmars_drawer_back_id);
            this.drawer_show = false;
        },
        onClose() {
            this.drawer_show = false;
        },
    },
};
</script>
